package cn.stylefeng.guns.modular.item.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.modular.item.Entity.Item;
import cn.stylefeng.guns.modular.item.mapper.ItemMapper;
import cn.stylefeng.guns.modular.item.service.IItemService;
import cn.stylefeng.guns.modular.item.wrapper.ItemWrapper;
import cn.stylefeng.guns.sys.core.log.LogObjectHolder;
import cn.stylefeng.roses.core.base.controller.BaseController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

/**
 * 物品管理控制器
 *
 * @author fengshuonan
 * @Date 2020-06-21 12:35:43
 */
@Controller
@RequestMapping("/item")
public class ItemController extends BaseController {

    private String PREFIX = "/item/";

    @Autowired
    private IItemService itemService;

    @Autowired
    private ItemMapper itemMapper;

    /**
     * 跳转到物品管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "item.html";
    }

    /**
     * 跳转到添加物品管理
     */
    @RequestMapping("/item_add")
    public String itemAdd() {
        return PREFIX + "item_add.html";
    }

    /**
     * 跳转到修改物品管理
     */
    @RequestMapping("/item_update/{itemId}")
    public String itemUpdate(@PathVariable Integer itemId, Model model) {
        Item item = itemService.getById(itemId);
        model.addAttribute("item",item);
        LogObjectHolder.me().set(item);
        return PREFIX + "item_edit.html";
    }

    /**
     * 获取物品管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(required = false) Integer itemId,
                       @RequestParam(required = false) String itemName,
                       @RequestParam(required = false) String type,
                       @RequestParam(required = false) Integer number) {
        List<Item> items = itemService.list();

        System.out.println();
        System.out.println(items);
        System.out.println();

        System.out.println("********************************************");
        System.out.println("********************************************");

        Page<Map<String,Object>> itemPages = itemMapper.listItems(LayuiPageFactory.defaultPage(),itemId,itemName,type,number);
//        for (int i = 0; i < items.size(); i++){
//            Item item = items.get(i);
//            itemPages.addOrder()
//        }
        Page<Map<String,Object>> mapPage = new ItemWrapper(itemPages).wrap();

        return LayuiPageFactory.createPageInfo(mapPage);
    }

//    @RequestMapping(value = "/list")
//    @ResponseBody
//    public Object list(@RequestParam(value = "itemId", required = false) String itemId) {
//        Page<Map<String, Object>> items = itemService.selectUsers();
//        System.out.println(LayuiPageFactory.defaultPage());
//        Page wrapped = new UserWrapper(items).wrap();
//        return LayuiPageFactory.createPageInfo(wrapped);
//    }

    /**
     * 新增物品管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Item item) {
        itemService.save(item);
        return SUCCESS_TIP;
    }

    /**
     * 删除物品管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer itemId) {
        itemService.removeById(itemId);
        return SUCCESS_TIP;
    }

    /**
     * 修改物品管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Item item) {
        itemService.updateById(item);
        return SUCCESS_TIP;
    }

    /**
     * 物品管理详情
     */
    @RequestMapping(value = "/detail/{itemId}")
    @ResponseBody
    public Object detail(@PathVariable("itemId") Integer itemId) {
        return itemService.getById(itemId);
    }
}
